// console.log("ok")

import Vue from 'vue'


// 导入router

import VueRouter from 'vue-router'

Vue.use(VueRouter)

import router from './router.js'

// 按需导入mitui的组件
// import Mint from 'mint-ui';
// import 'mint-ui/lib/style.css'
// Vue.use(Mint);

// 导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)
var car=JSON.parse(localStorage.getItem('car')||'[]')
const store =new Vuex.Store({
  state:{//相当于date
    car:car
  },
  mutations:{
    addTocar(state,goodsinfo){
      var flag=false
      state.car.some(item=>{
        if(item.id==goodsinfo.id){
          item.count+=parseInt(goodsinfo.count)
          flag=true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsinfo)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateGoodsInfo(state,goodsinfo){
      state.car.some(item=>{
        if(item.id==goodsinfo.id){
          item.count=parseInt(goodsinfo.count)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    removeCar(state,id){
      state.car.some((item,i)=>{
        if(item.id==id){
          state.car.splice(i,1)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateGoodsSelect(state,info){
      state.car.some(item=>{
        if(item.id==info.id){
          item.selected=info.selected
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters:{
      // 计算属性 
      getAllcount(state){
        var c=0
        state.car.forEach(item=>{
          c+=item.count
        })
        return c
      },
      getGoodscount(state){
        var a={}
        state.car.forEach(item=>{
          a[item.id]=item.count
        })
        return a
      },
      getGoodsselect(state){
        var o={}
        state.car.forEach(item=>{
          o[item.id]=item.selected
        })
        return o
      },
      getGoodsPrice(state){
        var o={
          count:0,//勾选的商品数量
          amount:0//勾选的总价
        }
        state.car.forEach(item=>{
          if(item.selected){
            o.count+=item.count
            o.amount+=item.count*item.price
          }
         
        })
        return o
      }

  },

})



// import { Header,Swipe, SwipeItem,Button,Lazyload } from 'mint-ui'
// Vue.component(Header.name,Header)
 import app from './App.vue'
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'


Vue.use(MintUI)

// 缩略图插件
import VuePreview from 'vue2-preview'
Vue.use(VuePreview)

// 导入时间插件
import moment from "moment"
// 设置过滤器
Vue.filter('dataFormat',function(dataStr,pattern="YYYY:MM:DD HH:mm:ss"){
  return moment(dataStr).format(pattern)
})

// 导入vue-resource
import VueRrsource from 'vue-resource'
Vue.use(VueRrsource)
Vue.http.options.emulateJSON = true;


// 导入mui组件
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'


var vm =new Vue({
  el:"#app",
  render:c=>c(app),
  router,
  store
})